package com.softline.documentsmall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.softline.documentsmall.dao.DictionaryRelationMapper;
import com.softline.documentsmall.entity.DictionaryRelationVo;
import com.softline.documentsmall.entity.WordVo;
import com.softline.documentsmall.service.DictionaryRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liusheng
 */
@Service
@Slf4j
public class DictionaryRelationServiceImpl implements DictionaryRelationService {

    @Autowired
    DictionaryRelationMapper dictionaryRelationMapper;

    @Override
    public JSONObject list(String type, Integer pageNum, Integer pageSize, String title) {
        JSONObject json = new JSONObject();
        int offset = (pageNum - 1)*pageSize;
        type = "name_"+type;
        List<DictionaryRelationVo> dictionaryRelationVoList = dictionaryRelationMapper.list(type,offset,pageSize,title);
        json.put("tableData",dictionaryRelationVoList);
        Integer totalCount = dictionaryRelationMapper.getTotal(title);
        JSONObject page = new JSONObject();
        if (totalCount%pageSize >0){
            page.put("totalPage",totalCount/pageSize+1);
        }else {
            page.put("totalPage",totalCount/pageSize);
        }
        page.put("totalCount",totalCount);
        page.put("pageNum",pageNum);
        page.put("pageSize",pageSize);
        json.put("page",page);
        return json;
    }

    @Override
    public JSONObject edit(List<WordVo> wordList, String dictionaryId) {
        JSONObject json = new JSONObject();
        dictionaryRelationMapper.deleteByDictionaryId(dictionaryId);
        dictionaryRelationMapper.insertBatch(wordList,dictionaryId);
        json.put("success",true);
        return json;
    }

    @Override
    public JSONObject editList(String dictionaryId) {
        JSONObject json = new JSONObject();
        List<WordVo> wordList = dictionaryRelationMapper.getWordList(dictionaryId);
        json.put("wordList",wordList);
        return json;
    }

}
